package com.hcrazy.manchester.app.jdbc;

import java.util.ArrayList;
import java.util.List;

import com.hcrazy.manchester.app.pojo.LiveAddress;
import com.hcrazy.manchester.app.pojo.LiveSimple;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.Secondary;

public class LiveSimpleDAO extends BaseDAO<LiveSimple>{

	public void addLiveSimple(LiveSimple simple) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("insert into him_live_simple(");
		sb.append("sortId,title,weblink,description_mc,details,praiseBaseCount,praiseCount,commentBaseCount,commentCount");
		sb.append(") values (");
		sb.append(":sortId,:title,:weblink,:description_mc,:details,:praiseBaseCount,:praiseCount,:commentBaseCount,:commentCount");
		sb.append(")");
		
		update(sb.toString(), simple);
	}
	
	public void updateLiveSimple(LiveSimple simple) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("update him_live_simple set ");
		sb.append(" sortId=:sortId,title=:title,weblink=:weblink,description_mc=:description_mc,");
		sb.append(" praiseBaseCount=:praiseBaseCount,praiseCount=:praiseCount,commentBaseCount=:commentBaseCount,commentCount=:commentCount,");
		sb.append(" details=:details");
		sb.append(" where infoId = :infoId");
		
		update(sb.toString(), simple);
	}
	
	public List<LiveSimple> listAllInfos() throws Exception{
		String sql = "select * from him_live_simple";
		return getList(sql, LiveSimple.class);
	}
	
	public List<LiveSimple> getAllInfosBySortId(int sortId) throws Exception{
		String sql = "select a.infoId,a.sortId,a.title,a.weblink,a.description_mc,b.sortName from him_live_simple a,him_live_sort b where a.sortId = b.sortId and a.sortId = ?";
		return getList(sql, LiveSimple.class,sortId);
	}
	
	public List<LiveSimple> getAllInfosHasDetailsBySortId(int sortId) throws Exception{
		String sql = "select a.*,b.sortName from him_live_simple a,him_live_sort b where a.sortId = b.sortId and a.sortId = ?";
		return getList(sql, LiveSimple.class,sortId);
	}
	
	public PageModel getPagedInfos(PageModel model,String search,int sortId) throws Exception{
		StringBuilder querySQL = new StringBuilder("select * from (select a.*,b.sortName from him_live_simple a,him_live_sort b where a.sortId = b.sortId) as c");
		StringBuilder countSQL = new StringBuilder("select count(*) from (select a.*,b.sortName from him_live_simple a,him_live_sort b where a.sortId = b.sortId) as c ");
		StringBuilder whereSQL = new StringBuilder(" where (title like ?) ");
		List<Object> paramList = new ArrayList<Object>();
		paramList.add("%"+search+"%");
		if(sortId > 0){
			whereSQL.append(" and sortId = ?");
			paramList.add(sortId);
		}
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, LiveSimple.class);
	}
	
	public LiveSimple getById(int infoId) throws Exception{
		String sql = "select * from him_live_simple where infoId = ?";
		return this.getJavaBean(sql, LiveSimple.class, infoId);
	}
	
	public LiveSimple getById(int infoId,boolean hasDetail) throws Exception{
		if(hasDetail){
			return getById(infoId);
		}
		
		String sql = "select * from him_live_simple_view where infoId = ?";
		return this.getJavaBean(sql, LiveSimple.class, infoId);
	}
	
	public void delLiveSimple(int infoId) throws Exception{
		String sql = "delete from him_live_simple where infoId = ?";
		this.commonUpdate(sql, infoId);
	}
	
	public long getPraiseCount(long infoId) throws Exception{
		String sql = "select praiseCount from him_live_simple where infoId = ?";
		return this.getJdbcTemplate().queryForLong(sql,infoId);
	}
	
	public long getPraiseBaseCount(long infoId) throws Exception{
		String sql = "select praiseBaseCount from him_live_simple where infoId = ?";
		return this.getJdbcTemplate().queryForLong(sql,infoId);
	}
	
	public long getCommentCount(long infoId) throws Exception{
		String sql = "select commentCount from him_live_simple where infoId = ?";
		return this.getJdbcTemplate().queryForLong(sql,infoId);
	}
	
	public long getCommentBaseCount(long infoId) throws Exception{
		String sql = "select commentBaseCount from him_live_simple where infoId = ?";
		return this.getJdbcTemplate().queryForLong(sql,infoId);
	}
	
	public void addPraiseCount(long infoId) throws Exception {
		String sql = "update him_live_simple set praiseCount = praiseCount + 1 where infoId = ?";
		this.commonUpdate(sql, infoId);
	}
	
	public void addCommentCount(long infoId) throws Exception {
		String sql = "update him_live_simple set commentCount = commentCount + 1 where infoId = ?";
		this.commonUpdate(sql, infoId);
	}
	
	/**
	 * 获取收藏信息
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public PageModel getCollegeInfos(long userId,int moduleType,PageModel model) throws Exception{
		String sql = "select a.type,b.* from him_user_collection a,him_live_simple b where a.infoId = b.infoId";
		StringBuilder querySQL = new StringBuilder("select * from ("+sql+") as m");
		StringBuilder countSQL = new StringBuilder("select count(*) from ("+sql+") as m");
		StringBuilder whereSQL = new StringBuilder(" where userId = ? and type = ?");
		
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(userId);
		paramList.add(moduleType);
		
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, LiveSimple.class);
	}
}
